// index.js
// 获取应用实例
const globalIp = getApp().globalData.userIp
const zhuzuo = getApp().globalData.zhuzuo
Page({
  data: {
    height: '',
    msgList: [],
    status: false,
    msgSet: {
      vertical: true, //滑动方向是否为纵向
      autoplay: true, //是否自动切换
      interval: 2300, //自动切换时间间隔
      duration: 500, //滑动动画时长
      circular: true, //是否采用衔接滑动
    },
    userInfo: '',
    bannerList:[{'cover':'https://sz.jimei123.com/upLoadImg/indexImg/home3.jpg'}
              ,{'cover':'https://sz.jimei123.com/upLoadImg/indexImg/home4.jpg'}
              ,{'cover':'https://sz.jimei123.com/upLoadImg/indexImg/home1.jpg'}
    ],
    recipe:[{'_id':'1','recipecover':'http://www.jimei123.com/media/upload/DIY/DIY-LAMINATION3.jpg','recipename':'DIY LAMINATION'},
            {'_id':'4','recipecover':'http://www.jimei123.com/media/upload/series/DIY/2.jpg','recipename':'DIY MAX'},
            {'_id':'7','recipecover':'http://www.jimei123.com/media/upload/series/DIY/1.jpg','recipename':'DIY UDLR'}
    ],
    recipe2:[{'_id':'2','recipecover':'http://www.jimei123.com/media/upload/index/Compress_J_COLORS-2.jpg','recipename':'玫瑰庄园'},
            {'_id':'5','recipecover':'http://www.jimei123.com/media/upload/index/Compress_J_COLORS-1.jpg','recipename':'迷失蔚蓝'},
            {'_id':'8','recipecover':'http://www.jimei123.com/media/upload/index/Compress_J_COLORS-4.jpg','recipename':'咖啡时间'},
            {'_id':'11','recipecover':'http://www.jimei123.com/media/upload/index/Compress_J_COLORS0.jpg','recipename':'默片时代'}
    ],
    recipe3:[{'_id':'3','recipecover':'http://www.jimei123.com/media/upload/2021%20SUMMER%20COLLECTION/JIMEI_UHD_20_Action_UHD_0109_0113_0001_1.jpg','recipename':'图案拼板UHD-S0112'},
            {'_id':'6','recipecover':'http://www.jimei123.com/media/upload/cms_14/2020%20%E7%A7%8B%E5%86%AC%E7%B3%BB%E5%88%97/%E7%BB%8F%E5%85%B8%E7%AF%87/JIMEI_UFQ_6_Delusion_UFQ_0031_0039_0001.jpg','recipename':'基础玳瑁UFQ-0034'},
            {'_id':'9','recipecover':'http://www.jimei123.com/media/upload/2023%20MIDO%20COLLECTION/JIMEI_UC_46_Fleeting_Time_UC2_0282_0289_0001_1.jpg','recipename':'挤板渐变UC2-0285'},
            {'_id':'12','recipecover':'http://www.jimei123.com/media/upload/2023%20MIDO%20COLLECTION/JIMEI_UN_92_Dreamland_UNP_0633_0640_0001_1.jpg','recipename':'挤板单色UNP-0637'}
    ],
    zhuzuo1: zhuzuo
  },
  toMsgDetail(e) {
    if (wx.getStorageSync('userInfo') && wx.getStorageSync('userInfo').LineStatus == 'Y') {
      wx.navigateTo({
        url: '/template/other/new/newIndex/newIndex?id='+e.currentTarget.dataset.id,
      })
    } else {
      wx.showToast({
        title: '手机授权且身份信息审核通过后，方可查看！',
        icon: 'none',
        duration: 2000,
      })
    }
  },

  onShareAppMessage(){
    // const promise = new Promise(resolve => {
    //   setTimeout(() => {
    //     resolve({
    //       title: '集美新材料'
    //     })
    //   }, 2000)
    // })
    // return {
    //   title: '集美新材',
    //   path: '/pages/index/index',
    //   promise 
    // }
  },
  onLoad() {
    var loginThis = this;
    wx.login({
      success(res) {
        if (res.code) {
          wx.request({
            url: globalIp + 'home/getOpenid',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json'
            },
            method: 'POST',
            success: function(res) {
              if (res.data.success) {
                console.log(res)
                loginThis.setData({
                  userInfo: res.data.userJson
                })
                wx.setStorageSync('userInfo', res.data.userJson) // 缓存userInfo
                console.log(wx.getStorageSync('userInfo'),'静默登录成功')
                loginThis.showNews();
                loginThis.newNews();
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            },
            fail: function(res) {
              wx.showToast({
                title: '获取信息失败，请重新进入小程序',
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else {
          wx.showToast({
            title: '获取信息失败，请重新进入小程序',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    // 开启禁止截图模式（安卓）
    // wx.setVisualEffectOnCapture({
    //   visualEffect: 'hidden',
    //   success:(res) => {
    //     console.log(res)
    //   },
    // })
  },

  showNews: function() {
    let loginThis = this;
    wx.request({
      url: globalIp + 'home/showNews',
      data: {
        value: 1,
        openId: wx.getStorageSync('userInfo').openid || '',
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success) {
          loginThis.setData({
            msgList: res.data.jsonObject,
            status: true
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        } 
      },
      fail: function(res) {
        wx.showToast({
          title: '请重新进入小程序',
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },

  newNews: function() {
    let that = this;
    wx.request({
      url: globalIp + 'home/showNews',
      data: {
        value: 4,
        openId: wx.getStorageSync('userInfo').openid || '',
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success) {
          console.log(res.data);
          let title = res.data.jsonObject[0].title;
          if (title != null && title != '') {
            wx.showModal({
              title: '公告提醒',
              content: title,
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  that.confirm();
                }
              }
            })
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        } 
      }
    })
  },

  confirm: function() {
    wx.request({
      url: globalIp + 'home/updateNewnews',
      data: {
        openId: wx.getStorageSync('userInfo').openid || '',
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (!res.data.success) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    this.setData({
      height: wx.getSystemInfoSync().screenHeight * 0.6
    })
  },
})
